#!/bin/bash

echo "这是一个使用包管理器更新的脚本"
echo "
1. apt
2. nala
3.更新脚本"

read -p "请输入1-3:" choice

if [ "$choice" = "1" ]; then
   echo "确认使用apt进行更新吗？"
   read -p "请输入 y/n：" confirm
   if [ "$confirm" = "y" ]; then
       echo "好的，正在使用apt更新..."
       apt update
       apt upgrade
   elif [ "$confirm" = "n" ]; then
       echo "好的，已取消更新。"
   else
       echo "无效的选择，请输入 y 或 n。"
   fi
elif [ "$choice" = "2" ]; then
   if command -v nala > /dev/null 2>&1; then
       echo "确认使用nala更新吗？"
       read -p "请输入 y/n：" confirmn
       if [ "$confirmn" = "y" ]; then
           echo "好的，正在使用nala更新..."
           nala update
           nala upgrade
           echo "是否运行nala fetch查找更快速的源? (y/n)"
           read -p "" nala_fetch
           if [ "$nala_fetch" = "y" ]; then
               nala fetch
               echo "是否立即执行换源操作? (/root/换源.sh) (y/n)"
               read -p "" change_source
               if [ "$change_source" = "y" ]; then
                   bash /root/换源.sh
               fi
           fi
       elif [ "$confirmn" = "n" ]; then
           echo "好的，已取消更新。"
       else
           echo "无效的选择，请输入 y 或 n。"
       fi
   else
       echo "未找到nala工具，是否现在安装?(y/n)"
       read -p "" install_nala
       if [ "$install_nala" = "y" ]; then
           echo "正在安装nala..."
           apt update && apt install nala
           echo "nala已成功安装。"
           # 安装完后直接使用nala更新
           echo "正在使用nala更新..."
           nala update
           nala upgrade
       else
           echo "好的，将默认使用apt更新..."
           echo "确认使用apt进行更新吗？"
           read -p "请输入 y/n：" confirm
           if [ "$confirm" = "y" ]; then
               echo "好的，正在使用apt更新..."
               apt update
               apt upgrade
           elif [ "$confirm" = "n" ]; then
               echo "好的，已取消更新。"
           else
               echo "无效的选择，请输入 y 或 n"
           fi
       fi
   fi
elif [ "$choice" = "3" ]; then
   if command -v nala > /dev/null 2>&1; then
   echo "选择更新的站点
   1:gitee(推荐)
   2:github"
   read -p "请输入1-2" zhandian
   if [ "$zhandian" = "1" ]; then
   echo "正在从gitee下载脚本文件"
   git clone git@gitee.com:yalishidoordea/huanyuan.git
   echo "完成，正在退出"
   fi
   elif [ "$zhandian" = "2" ]; then
   echo "正在从github下载脚本文件"
   git clone git@gitee.com:yalishidoordea/huanyuan.git
   echo "完成，正在退出"
   else
   echo "无效的选择，请输入正确的数字！"
   exit 1
   fi
   echo "已退出"
   exit
else
    echo "无效的选择，请输入1或2。"
fi
echo "更新完毕,感谢使用！！
      作者：Yalishidoorde"
exit